package com.example.foodisimportant.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodisimportant.Bean.MessageEntity;
import com.example.foodisimportant.Bean.PostEntity;
import com.example.foodisimportant.R;

import java.util.ArrayList;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.myViewHolder> {
    private Context context;
    private ArrayList<MessageEntity> messageEntities;
    //创建构造函数
    public MessageAdapter(Context context, ArrayList<MessageEntity> messageEntities){
        //将传递过来的数据赋值给本地变量
        this.context=context;//上下文
        this.messageEntities=messageEntities;//实体类数据
    }

    /**
     * 创建viewholder，相当于listview中的getview中创建view和viewholder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MessageAdapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建自定义布局
        View itemView=View.inflate(context, R.layout.item3,null);
        return new MessageAdapter.myViewHolder(itemView);
    }

    /**
     * 绑定数据，数据与view绑定
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull MessageAdapter.myViewHolder holder, int position) {
        MessageEntity data=messageEntities.get(position);
        holder.mItem3name.setText(data.name);
        holder.mItem3preview.setText(data.preview);
        holder.mItem3time.setText(data.time);
    }


    @Override
    public int getItemCount() {
        return messageEntities.size();
    }

    public class myViewHolder extends RecyclerView.ViewHolder {
        private TextView mItem3name;
        private TextView mItem3preview;
        private TextView mItem3time;
        private ImageView mItem3Picture;

        public myViewHolder(View itemView) {
            super(itemView);
            mItem3name=itemView.findViewById(R.id.item3_name);
            mItem3preview=itemView.findViewById(R.id.item3_preview);
            mItem3time=itemView.findViewById(R.id.item3_time);
            mItem3Picture=itemView.findViewById(R.id.item3_touxiang);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener!=null){
                        onItemClickListener.OnItemClick(v,messageEntities.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    /**
     * 设置item的监听事件
     */
    public interface OnItemClickListener{
        public void OnItemClick(View view, MessageEntity data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private MessageAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(MessageAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

}
